package com.meij.fragment;

import org.json.JSONObject;
import org.lucasr.twowayview.ItemClickSupport;
import org.lucasr.twowayview.ItemClickSupport.OnItemClickListener;
import org.lucasr.twowayview.ItemClickSupport.OnItemLongClickListener;
import org.lucasr.twowayview.widget.DividerItemDecoration;
import org.lucasr.twowayview.widget.TwoWayView;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import cn.pedant.SweetAlert.SweetAlertDialog;

import com.fragment.BaseFragment;
import com.meij.MJApplaction;
import com.meij.R;
import com.meij.activity.PicActivity;
import com.meij.activity.user.UserCenterActivity;
import com.meij.adapter.PicListAdapter;
import com.meij.bean.Album;
import com.meij.http.Json;
import com.meij.http.MJNetCallBack;
import com.meij.http.Url;

public class PicListFragment extends BaseFragment {
	private TwoWayView mRecyclerView;
	private PicListAdapter adapter;
	private UserCenterActivity activity;

	@Override
	protected int getID() {
		return R.layout.fragment_piclist;
	}

	@Override
	protected void editView() {
		activity = (UserCenterActivity) getActivity();
		mRecyclerView = (TwoWayView) viewGroup.findViewById(R.id.list);
		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setLongClickable(true);
		final ItemClickSupport itemClick = ItemClickSupport.addTo(mRecyclerView);
		itemClick.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(RecyclerView parent, View view, int position, long id) {
				if (adapter.isCurrentUser()) {
					switch (position) {
					case 0:
						return false;
					default:
						delPic(position - 1);
						return true;
					}
				} else {
					delPic(position);
					return true;
				}
			}
		});
		itemClick.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(RecyclerView parent, View child, int position, long id) {
				if (adapter.isCurrentUser()) {
					switch (position) {
					case 0:
						activity.setPic();
						break;
					default:
						showPic(position - 1);
						break;
					}
				} else {
					showPic(position);
				}
			}
		});
		final Drawable divider = getResources().getDrawable(R.drawable.divider);
		mRecyclerView.addItemDecoration(new DividerItemDecoration(divider));
		adapter = new PicListAdapter(activity, activity.albums, activity.id);
		mRecyclerView.setAdapter(adapter);
	}

	/***
	 * 显示图片
	 */
	public void showPic(int position) {
		Intent intent = new Intent(activity, PicActivity.class);
		intent.putExtra("albums", activity.albums);
		intent.putExtra("position", position);
		startActivity(intent);
	}

	/***
	 * 删除图片
	 */
	public void delPic(final int position) {
		new SweetAlertDialog(activity, SweetAlertDialog.WARNING_TYPE).setTitleText("删除图片？").setCancelText("否").setConfirmText("是")
				.showCancelButton(true).setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
					@Override
					public void onClick(SweetAlertDialog sDialog) {
						sDialog.dismiss();
					}
				}).setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
					@Override
					public void onClick(final SweetAlertDialog sDialog) {
						Album album = activity.albums.get(position);
						String url = Url.photo_remove + "?photoId=" + album.getId();
						MJNetCallBack callBack = new MJNetCallBack(activity) {
							@Override
							public void onStart() {
							}

							@Override
							public void onSuccess(JSONObject object) {
								sDialog.setTitleText("成功!").showCancelButton(false).setConfirmText("好的").setConfirmClickListener(null)
										.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
								activity.albums.remove(position);
								adapter.notifyDataSetChanged();
							}

							@Override
							public void onFailure(String message) {
								sDialog.dismiss();
							};
						};
						activity.getJson(url, callBack);

					}
				}).show();
	}

	public void getData() {
		String url = Url.detail + "?id=" + MJApplaction.userInfo.getUserkey();
		MJNetCallBack callBack = new MJNetCallBack(activity) {
			@Override
			public void onSuccess(JSONObject object) {
				super.onSuccess(object);
				activity.albums.clear();
				activity.albums.addAll(Json.getAlbums(object));
				adapter.notifyDataSetChanged();
			}
		};
		activity.getJson(url, callBack);
	}

}
